// pages/collect/collect.js
const app = getApp()
var config = require('../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '',
    page:1,
    pageSize:10,
    detailList:[]
  },
  expend:function(){
    wx.navigateTo({
      url: '../expend/expend',
    })
  },
  income:function(e){
    var workerWageFlowId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../income/income?workerWageFlowId=' + workerWageFlowId,
    })
  },
  bindDateChange: function (e) {
    this.setData({
      detailList: [],
      date: e.detail.value,
      page:1
    })
    this.queryList(e.detail.value);
  },
  /**
   * 上拉加载
   */
  onReachBottom: function () {
    var that = this;
    var page = that.data.page;
    page = page + 1;
    that.setData({
      page: page
    })
    var date=that.data.date;
    that.queryList(date);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var initializeDate = Y + '-' + M;
    that.setData({
      date: initializeDate
    })
    that.queryList(initializeDate);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取列表信息
   */
  queryList: function (initializeDate) {
    wx.showLoading({
      title: '加载中',
    })
    var that=this;
    var workerCode = wx.getStorageSync("workerInfo").workerCode;
    var searchTime = that.data.date;
    var startIndex = that.data.page;
    var pageSize = that.data.pageSize;
    var detailList = that.data.detailList;
    let searchData = {
      param: '{' +
        '"workerCode":"' + workerCode + '",' +
        '"searchTime":"' + searchTime + '",' +
        '"startIndex":"' + startIndex + '",' +
        '"pageSize": "' + pageSize + '"}'
    }
    wx.request({
      url: config.queryJournalAccountList,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: searchData,
      success: function (result) {
        wx.hideLoading();
        console.info("收支列表查询返回结果");
        console.info(result);
        var data = result.data.object;
        detailList = detailList.concat(data.rows);
        console.info(detailList)
        that.setData({
          totalExpenditure: data.totalExpenditure,
          totalIncome: data.totalIncome,
          detailList: detailList
        });

      },
      fail: function ({ errMsg }) {
        console.info("收支列表查询失败");
        wx.hideLoading();
      },
    });
  }
})